package com.cloudptt.api.pjsua2;

import com.cloudptt.api.product.log.Log;

import org.pjsip.pjsua2.Buddy;
import org.pjsip.pjsua2.BuddyConfig;
import org.pjsip.pjsua2.BuddyInfo;
import org.pjsip.pjsua2.pjsip_evsub_state;
import org.pjsip.pjsua2.pjsua_buddy_status;

public class MyBuddy extends Buddy
{
    public static String THIS_FILE = "MyBuddy";
    public BuddyConfig cfg;

    public MyBuddy(BuddyConfig config)
    {
        super();
        cfg = config;
        Log.d(THIS_FILE, "MyBuddy config = " + config);
    }

    public String getStatusText()
    {
        BuddyInfo bi;

        try {
            bi = getInfo();
        } catch (Exception e) {
            return "?";
        }

        String status = "";
        if (bi.getSubState() == pjsip_evsub_state.PJSIP_EVSUB_STATE_ACTIVE) {
            if (bi.getPresStatus().getStatus() ==
                    pjsua_buddy_status.PJSUA_BUDDY_STATUS_ONLINE)
            {
                status = bi.getPresStatus().getStatusText();
                if (status == null || status.length()==0) {
                    status = "Online";
                }
            } else if (bi.getPresStatus().getStatus() ==
                    pjsua_buddy_status.PJSUA_BUDDY_STATUS_OFFLINE)
            {
                status = "Offline";
            } else {
                status = "Unknown";
            }
        }
        return status;
    }

    @Override
    public void onBuddyState()
    {
//        MyApp.observer.notifyBuddyState(this);
    }

    public String toString()
    {
        StringBuffer strBuf = new StringBuffer();
        strBuf.append("Buddy").append(";");
        strBuf.append("Valid").append("=").append(this.isValid()).append(";");
        try {
            strBuf.append("Info").append("=").append(this.getInfo()).append(";");
        }
        catch (Exception e)
        {}
        return strBuf.toString();
    }

}
